import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import viteImagemin from "vite-plugin-compression";
import viteRemoveConsole from "vite-plugin-compression";
import viteCompression from "vite-plugin-compression";

function myPlugin() {
  return {
    name: "myPlugin",
    transform(code: string, id: string) {
      if (id.includes("node_modules")) {
        return code;
      }
      return code.replace(/console\.log\([^)]*\);?/g, "");
    },
  };
}

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    react(),
    myPlugin(),
    viteImagemin(),
    viteRemoveConsole(),
    viteCompression({
      //压缩算法
      algorithm: "gzip",
    }),
  ],
  resolve: {
    alias: {
      "@": "/src",
    },
  },
  server: {
    proxy: {
      "/apis": {
        target: "http://127.0.0.1:8087/",
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/apis/, ""),
      },
      "/api": {
        target: "https://openapi.youdao.com",
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ""),
      },
    },
  },
});
